<script>
export default {
  data() {
    return {
      doctorForm: {
      },
      imageUrl:'',
      bt:'',
    }
  },
  methods: {
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.doctorForm.img=res;
    },
    saveDoctor() {
      // 发送保存医生信息的请求到后端
      this.axios.post(`http://localhost:8302/doctor/save`,this.doctorForm).then(res=>{
        this.$router.push('/doctor');
        this.$message({
          type: 'success',
          message: '保存成功!'
        });
      })
    },
    goBack() {
      // 返回上一页
      this.$router.go(-1);
      this.bt=""
      this.doctorForm={}
    },
    finddoctor(){
      var doctor=JSON.parse(localStorage.getItem("doctor"))
      if(doctor!=null){
        localStorage.removeItem("doctor")
        this.bt="编辑医生信息"
        this.doctorForm=doctor;
        this.imageUrl=doctor.img
      }else {
        this.bt="添加医生信息"
      }
    }
  },
  created() {
    // 在页面创建时获取医生信息
    this.finddoctor();

  }
}
</script>

<template>
  <div style="width: 100%;margin-left: 40px;margin-right: 40px" align="center;">
    <div style="background-color: #F2F7FB;padding: 10px 0px 0px 0px" >
      <el-header align="left" style="margin-top: 60px;"> | {{ bt }}</el-header>

      <el-main align="left">
        <el-form :model="doctorForm" label-width="120px">
          <el-form-item v-if="doctorForm.id!=null" label="医生编号">
            <el-input v-if="doctorForm.id!=null" v-model="doctorForm.id" disabled></el-input>
          </el-form-item>
          <el-form-item label="账号">
            <el-input v-model="doctorForm.phone" placeholder="请输入有效手机号码"></el-input>
          </el-form-item>
          <el-form-item label="医生头像">
            <el-upload
                class="avatar-uploader"
                action="http://localhost:8302/bao/tp"
                name="file"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload">
              <img width="50px" v-if="imageUrl" :src="imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="医生姓名">
            <el-input v-model="doctorForm.name" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="医生角色">
            <el-select v-model="doctorForm.role" placeholder="请选择">
              <!-- 这里可以添加医生角色选项 -->
              <el-option label="系统管理员" value="系统管理员"></el-option>
              <el-option label="全科医师" value="全科医师"></el-option>
              <el-option label="全科护士" value="全科护士"></el-option>
              <el-option label="护理医师" value="护理医师"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="登录密码">
            <el-input v-model="doctorForm.password" placeholder="请输入" type="password"></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <el-select v-model="doctorForm.sex" placeholder="请选择">
              <el-option label="男" value="男"></el-option>
              <el-option label="女" value="女"></el-option>
              <!-- 这里可以添加性别选项 -->
            </el-select>
          </el-form-item>
          <el-form-item label="医生简介">
            <el-input v-model="doctorForm.introduce" type="textarea" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="医生擅长">
            <el-input v-model="doctorForm.speciality" type="textarea" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button style="width: 100px;" type="primary" @click="saveDoctor">保存</el-button>
            <el-button style="width: 100px;" @click="goBack">返回</el-button>
          </el-form-item>
        </el-form>
      </el-main>
    </div>
  </div>
</template>

<style>

</style>
